package cn.xshi.sys.web;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.xshi.sys.model.SysSms;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.base.BaseSearch;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.sys.service.SysSmsService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
/**
 * @Desc 短信配置
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/sysSms")
@Api(value = "短信配置API",tags = "短信配置API",description = "短信配置API")
public class SysSmsController extends BaseAction {

	@Resource
	private SysSmsService sysSmsService;

	/**
	* 查询短信列表并分页
	* @param baseSearch
	*/
	@ApiOperation(value="查询短信列表并分页", notes="查询短信列表并分页")
	@PostMapping(value="/list")
	@Auth(value = "/sysSms/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BasePage<List<SysSms>> getSysSmsListByCondition(@RequestBody(required=true)BaseSearch baseSearch){
		Map<String, Object> condition = baseSearch.convert();
		commonHPager(baseSearch);
		List<SysSms> sysSmsList = sysSmsService.getSysSmsListByCondition(condition);
		for(SysSms sysSms : sysSmsList){
			if(!StringUtil.isEmpty(sysSms.getCreateId())){
				OauthAccountEntity createBy = getAccount(sysSms.getCreateId());
				if(null != createBy){
					sysSms.setCreateBy(createBy.getName());
				}
			}
			if(!StringUtil.isEmpty(sysSms.getUpdateId())){
				OauthAccountEntity modifiedBy = getAccount(sysSms.getUpdateId());
				if(null != modifiedBy){
					sysSms.setModifiedBy(modifiedBy.getName());
				}
			}
		}
		PageInfo<SysSms> page = new PageInfo<SysSms>(sysSmsList);
		return outPageBootStr(page,baseSearch);
	}

	/**
	* 查询单个短信
	* @param id
	*/
	@ApiOperation(value="查询单个短信", notes="查询单个短信")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/sysSms/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<SysSms> getSysSmsById(@PathVariable("id")String id){
		SysSms sysSms = sysSmsService.getSysSmsById(id);
		if(!StringUtil.isEmpty(sysSms.getCreateId())){
			OauthAccountEntity createBy = getAccount(sysSms.getCreateId());
			if(null != createBy){
				sysSms.setCreateBy(createBy.getName());
			}
		}
		if(!StringUtil.isEmpty(sysSms.getUpdateId())){
			OauthAccountEntity modifiedBy = getAccount(sysSms.getUpdateId());
			if(null != modifiedBy){
				sysSms.setModifiedBy(modifiedBy.getName());
			}
		}
		return outDataStr(sysSms);
	}

	/**
	* 添加
	* @param sysSms
	*/
	@PostMapping(value="/add")
	@ApiOperation(value="创建单个短信", notes="创建单个短信")
	public BaseResult addSysSms(@RequestBody SysSms sysSms){
		int i = 0;
		if(null != sysSms){
			i= sysSmsService.addSysSms(sysSms);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
	
	/**
	* 修改
	* @param sysSms
	*/
	@PutMapping(value="/update")
	@ApiOperation(value="编辑单个短信", notes="编辑单个短信")
	public BaseResult updateSysSms(@RequestBody SysSms sysSms){
		int i = 0;
		if(null != sysSms){
			i= sysSmsService.updateSysSms(sysSms);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 删除
	* @param id
	*/
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除短信", notes="删除短信")
	public BaseResult delSysSms(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i= sysSmsService.delSysSms(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
}
